/**
 * Created by luyon on 2017/8/23.
 */


"use strict";

import React from "react" ;
import {connect} from "react-redux" ;
import {mapStateToProps, mapDispatchToProps} from "./FilmCreatorRedux" ;
import {browserHistory} from "react-router" ;
import tx from "../../../../static/img/film/moren-tx.png"
import V from "../../../../static/img/film/geren-V.png";
import qiteV from "../../../../static/img/film/qite-V.png";

import UrlManager from "../../../../utils/UrlManager";
import NetworkEngine from "../../../../utils/NetworkEngine" ;

class FilmCreatorContainer extends React.Component {
    constructor(p) {
        super(p);
    }

    componentDidMount() {

    }
    turn2LoginPage(isNotFollow) {
        let data = this.props.filmCreatorInfomation;
        isNotFollow = data.isNotFollow;
        if (isNotFollow === "0") {
            browserHistory.push("/");
        } else if (isNotFollow === "1") {
            let urlManager = new UrlManager();
            NetworkEngine(
                "post",
                urlManager.unfollow(),
                {
                    "followerId": sessionStorage.getItem("id"),//粉丝 也就是当前用户
                    "userId": data.userId,//作者
                    "attentionType": "1",
                    userKeyId:sessionStorage.setItem("userKeyId")
                }
            ).then(
                data => {
                    if (data.data.attentionType === "0") {
                            this.props.fresh();
                    }
                }
            ).catch(
                err => {
                    //失败执行
                }
            )

        } else if (isNotFollow === "2") {
            //关注接口
            let urlManager = new UrlManager();
            NetworkEngine(
                "post",
                urlManager.follow(),
                {
                    "followerId": sessionStorage.getItem("id"),//粉丝 也就是当前用户
                    "userId": data.userId,//作者
                    attentionType: "0",
                    userKeyId:sessionStorage.setItem("userKeyId")


                }
            ).then(
                data => {
                    let _data = data.data.attentionType;
                    _data = "1";
                    if (_data == 1) {
                        this.props.fresh();
                        this.props.showAttentionTip();
                    }
                }
            );
        }

        return;
    }
    //进入个人主页
    queryCreator() {
        localStorage.setItem("personalId",this.props.filmCreatorInfomation.userId)
        var id =this.props.filmCreatorInfomation.userId;
            id = id.replace(/\//g, '_');
        var viewerKey = sessionStorage.getItem("viewerKey");
        if(viewerKey == null || viewerKey == ""){
            viewerKey = "0";
        }
        browserHistory.push("/App/MobileMine/" + id+"/"+viewerKey);
        window.scrollTo(0,0);
    }
    closeModel(){
        $(this.refs.cancel_model).hide();
    }

    cancelAttention(){
        let isNotFollow = this.props.filmCreatorInfomation.isNotFollow;
        this.turn2LoginPage(isNotFollow)
        $(this.refs.cancel_model).hide();
    }

    followClick(){
        $(this.refs.cancel_model).show()
    }


    render() {
        let data = this.props.filmCreatorInfomation;
        let isNotFollow = data.isNotFollow;  //0未登录  1已关注  2未关注  3自己
        let TestHtml = () => {
            if (isNotFollow === "0") {
                return <button className="mz-btn button" onClick={this.turn2LoginPage.bind(this, isNotFollow)}>
                    +关注</button>
            } else if (isNotFollow === "1") {
                return <button className="mz-btn button" onClick={this.followClick.bind(this)}>
                    已关注</button>
            } else if (isNotFollow === "2") {
                return <button className="mz-btn button" onClick={this.turn2LoginPage.bind(this, isNotFollow)}>
                    +关注</button>
            } else {
                return <div></div>
            }
        }
        let Logo = () => {
            if (data.authenticationType == "0") {
                return <img src={V} alt="图标"/>
            } else {
                return <img src={qiteV} alt="图标"/>
            }
        }

        return (
            <div className="player-writer clearfix">
                <div className="writer-img" >
                    <img src={data.headUrl} alt="作者头像" onClick={this.queryCreator.bind(this)}/>
                </div>
                <ul className="writer-text">
                    <li className="text1">{data.nickName}
                        <Logo/>
                    </li>
                    <li className="text2">{data.occupationName}</li>
                    <li className="text3">{data.introduction}</li>
                </ul>
            </div>

        )
    }
}

const FilmCreator = connect(mapStateToProps, mapDispatchToProps)(FilmCreatorContainer);

export {FilmCreator as default} ;